package com.zxmr.component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.rabbitmq.client.AMQP;
import com.zxmr.action.QueueWithExchangeReceiver;
import com.zxmr.domain.component.Component;
import com.zxmr.navigate.RequestDispatch;
import com.zxmr.navigate.URLElement;
import com.zxmr.navigate.XMLConfigurationLoader;

public class Server {

	private final static String QUEUE_NAME = "hello1";
	
	public static void main(String[] args) throws IOException{
		/*
		// TODO Auto-generated method stub
		ConnectionFactory factory = new ConnectionFactory();
	   // factory.setHost("localhost");
		factory.setVirtualHost("/");
	    Connection connection = factory.newConnection();
	    final Channel channel = connection.createChannel();
	    
	    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
	    channel.exchangeDeclare("hello", "direct");
	    
	    channel.queueBind(QUEUE_NAME, "hello", "hello");
	    
	    final QueueingConsumer consumer = new QueueingConsumer(channel);
	    channel.basicConsume(QUEUE_NAME, true, consumer);
	    
	    try
	    {
		    while (true) {
		    	System.out.println("consumer tag is "+consumer.getConsumerTag());
		      QueueingConsumer.Delivery delivery = consumer.nextDelivery();
		      System.out.println("sssss");
		        AMQP.BasicProperties requestProperties = delivery.getProperties();
		        String correlationId = requestProperties.getCorrelationId();
		        String replyTo = requestProperties.getReplyTo();
		        
		      System.out.println("source routing key is "+replyTo+" correlation id is "+correlationId);
		      String message = new String(delivery.getBody());
		      System.out.println(" [x] Received '" + message + "'");
		      
		      System.out.println("sending response");
	          byte[] replyBody = "your response".getBytes();
	          
	          AMQP.BasicProperties prop = buildProp(correlationId,replyTo);
	          channel.basicPublish("", replyTo, prop, replyBody);
		    }
	    }catch(Exception e)
	    {
	    	e.printStackTrace();
	    	System.out.println("closed");
	    	channel.close();
	    	connection.close();
	    	es.shutdownNow();
	    }
	    */
		
		Component component = new Component();
		component.setName("hello");
		
		ExecutorService es = Executors.newCachedThreadPool();
		
		QueueWithExchangeReceiver receiver = new QueueWithExchangeReceiver(es,"hello");
		Thread t = new Thread(receiver);
		t.start();

		List<URLElement> elements = new ArrayList<URLElement>();
		URLElement element = new URLElement();
		element.setClassPath("com.zxmr.client.TestExecutor");
		element.setValue("/component/hello");
		elements.add(element);
		
		XMLConfigurationLoader loader = new XMLConfigurationLoader();
		loader.setUrls(elements);
		
		RequestDispatch.init(loader);
		
	}
	
	private static AMQP.BasicProperties buildProp(String correlationId,String srcQueue)
	{
		AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
		return builder.correlationId(correlationId).replyTo(srcQueue).build();
	}

}
